package com.welove.activity;

import com.welove.R;
import com.welove.fragments.FriendFragment;
import com.welove.fragments.MessageFragment;
import com.welove.fragments.MoreFragment;
import com.welove.fragments.SearchFragment;
import com.welove.fragments.VipFragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;

public class MainActivity extends BaseActivity implements
		OnCheckedChangeListener, OnTabChangeListener {

	private TabHost mTabHost;
	private RadioButton rbfri, rbsear, rbmsg, rbvip,rbmore;

	FragmentManager fm;
	FriendFragment friFragment;
	SearchFragment searFragment;
	MessageFragment msgFragment;
	VipFragment vipFragment;
	MoreFragment moreFragment;

	private void initView() {

		rbfri = (RadioButton) findViewById(R.id.radio_button0);
		rbsear = (RadioButton) findViewById(R.id.radio_button1);
		rbmsg = (RadioButton) findViewById(R.id.radio_button2);
		rbvip = (RadioButton) findViewById(R.id.radio_button3);
		rbmore = (RadioButton) findViewById(R.id.radio_button4);
		setupIntent();

		rbfri.setOnCheckedChangeListener(this);
		rbsear.setOnCheckedChangeListener(this);
		rbmsg.setOnCheckedChangeListener(this);
		rbvip.setOnCheckedChangeListener(this);
		rbmore.setOnCheckedChangeListener(this);

	}
	
	@Override
	protected void installViews() {
		// TODO Auto-generated method stub
		setContentView(R.layout.activity_lovemain);
		fm = getSupportFragmentManager();
		
		
		friFragment = (FriendFragment) fm
				.findFragmentByTag("friend");
		 searFragment = (SearchFragment) fm
				.findFragmentByTag("search");
		 msgFragment = (MessageFragment) fm
				.findFragmentByTag("message");
		 vipFragment = (VipFragment) fm
				.findFragmentByTag("vip");
		
		 moreFragment = (MoreFragment) fm
				.findFragmentByTag("more");
		mTitleBar.titlebar_btn_left.setVisibility(View.GONE);
		mTitleBar.titlebar_btn_right.setVisibility(View.GONE);
		mTitleBar.titlebar_tv_center.setText("缘分");
		initView();
	}

	@Override
	protected void registerEvents() {
		// TODO Auto-generated method stub
		
	}

	private void setupIntent() {

		mTabHost = (TabHost) findViewById(android.R.id.tabhost);

		TabHost localTabHost = this.mTabHost;
		localTabHost.setup();
		localTabHost.setOnTabChangedListener(this);
		localTabHost.addTab(buildTabSpec("friend", R.string.main_shouye));
		localTabHost.addTab(buildTabSpec("search", R.string.main_fujin));
		localTabHost.addTab(buildTabSpec("message", R.string.main_fenlei));
		localTabHost.addTab(buildTabSpec("vip", R.string.main_my));
		localTabHost.addTab(buildTabSpec("more", R.string.main_my));

		localTabHost.setCurrentTab(0);
	}

	private TabHost.TabSpec buildTabSpec(String tag, int resLabel) {
		return this.mTabHost.newTabSpec(tag).setIndicator(getString(resLabel))
				.setContent(new DummyTabContent(getBaseContext()));
		
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		// TODO Auto-generated method stub
		if (isChecked) {
			switch (buttonView.getId()) {
			case R.id.radio_button0:
				mTabHost.setCurrentTabByTag("friend");
				rbmsg.setChecked(false);
				rbsear.setChecked(false);
				rbvip.setChecked(false);
				rbmore.setChecked(false);
				break;
			case R.id.radio_button1:
				mTabHost.setCurrentTabByTag("search");
				rbmsg.setChecked(false);
				rbfri.setChecked(false);
				rbvip.setChecked(false);
				rbmore.setChecked(false);
				break;
			case R.id.radio_button2:
				mTabHost.setCurrentTabByTag("message");
				rbsear.setChecked(false);
				rbfri.setChecked(false);
				rbvip.setChecked(false);
				rbmore.setChecked(false);
				break;
			case R.id.radio_button3:
				mTabHost.setCurrentTabByTag("vip");
				
				rbmsg.setChecked(false);
				rbsear.setChecked(false);
				rbfri.setChecked(false);
				
				rbmore.setChecked(false);
				break;
			case R.id.radio_button4:
				mTabHost.setCurrentTabByTag("more");
				rbmsg.setChecked(false);
				rbsear.setChecked(false);
				rbfri.setChecked(false);
				rbvip.setChecked(false);
				
				break;

			}
		}
	}

	@Override
	public void onTabChanged(String tabId) {
		// TODO Auto-generated method stub
		
		

		FragmentTransaction ft = fm.beginTransaction();

		if (friFragment != null)
			ft.detach(friFragment);
		if (searFragment != null)
			ft.detach(searFragment);

		if (msgFragment != null)
			ft.detach(msgFragment);
		if (vipFragment != null)
			ft.detach(vipFragment);
		if (moreFragment != null)
			ft.detach(moreFragment);
		

		if (tabId.equalsIgnoreCase("friend")) {

			if (friFragment == null) {
				friFragment=new FriendFragment();

				ft.add(R.id.realtabcontent, friFragment, "friend");

			} else {

				ft.attach(friFragment);
			}

		} else if (tabId.equalsIgnoreCase("search")) {
			if (searFragment == null) {
				searFragment=new SearchFragment();
				ft.add(R.id.realtabcontent, searFragment, "search");
			} else {

				ft.attach(searFragment);
			}
		} else if (tabId.equalsIgnoreCase("message")) {
			if (msgFragment == null) {
				msgFragment=new MessageFragment();
				ft.add(R.id.realtabcontent,msgFragment , "message");
			} else {

				ft.attach(msgFragment);
			}
		} else if (tabId.equalsIgnoreCase("vip")) {
			if (vipFragment == null) {
				vipFragment=new VipFragment();
				ft.add(R.id.realtabcontent,vipFragment , "vip");
			} else {

				ft.attach(vipFragment);
			}
		}
		else if (tabId.equalsIgnoreCase("more")) {
			if (moreFragment == null) {
				moreFragment=new MoreFragment();
				ft.add(R.id.realtabcontent,moreFragment , "more");
			} else {

				ft.attach(moreFragment);
			}
		}
		
		ft.commit();
	}

	
}
